package com.framework.dubbo.producer.command;

import cn.hutool.core.util.StrUtil;
import com.framework.dubbo.producer.config.Pdf2HtmlProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * 描述：
 * 多例模式 windows liunx
 * @author xianggj
 * @Date 2021/8/25 10:50
 **/
@Slf4j
public class CommandHandler {

    private CommandHandler() {
    }

    /**
     * window环境对象
     */
    private static CommandHandler windowsCommand = new CommandHandler();

    /**
     * liunx环境对象
     */
    private static CommandHandler liunxCommand = new CommandHandler();

    /**
     * 组装命令
     * @return
     */
    public String assembleCommand(Pdf2HtmlProperties pdf2HtmlProperties, String sourceFilePath, String distDir) {
        return assembleCommand(pdf2HtmlProperties, sourceFilePath, distDir, null);
    }

        /**
         * 组装命令
         * @return
         */
    public String assembleCommand(Pdf2HtmlProperties pdf2HtmlProperties, String sourceFilePath, String distDir, String distFileName){
        if (pdf2HtmlProperties == null || StrUtil.isBlank(sourceFilePath) ||
            StrUtil.isBlank(distDir)
        ){
            throw new IllegalArgumentException("参数不齐全");
        }
         String command = null;
        if (this.equals(windowsCommand)){
            command = pdf2HtmlProperties.getWindowCommand();
        } else {
            command = pdf2HtmlProperties.getCommand();
        }
        if (command == null){
            throw new IllegalArgumentException("命令没有配置");
        }
        String line = String.format("%s --dest-dir %s %s", command, distDir, sourceFilePath);
        if (StrUtil.isNotBlank(distFileName)){
            line =  line + " " + distFileName;
        }
        log.info("command:" + line);
        return line;

    }

    /**
     * 获取对象时进行加锁，只能锁这里
     * @return
     */
    public synchronized static CommandHandler getInstance(){
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")){
            System.out.println("this is wins");
            return windowsCommand;
        } else {
            System.out.println("this is linux");
            return liunxCommand;
        }
    }

}

